'use strict';

exports.__esModule = true;

var _react = require('react');

var _reactImmutableRenderMixin = require('react-immutable-render-mixin');

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * 项目中的component除纯函数化组建外，都应该继承该类。
 * @class BaseComponent
 * @extends React.Component
 * @memberOf fe-lib
 */
var BaseComponent = function (_Component) {
  _inherits(BaseComponent, _Component);

  function BaseComponent() {
    _classCallCheck(this, BaseComponent);

    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }

  /**
   * 基于immutable判断，覆写shouldComponentUpdate方法
   * @function shouldComponentUpdate
   * @memberof BaseComponent
   * @instance
   * @param nextProps -- 最新props数据
   * @param nextState -- 最新state数据
   * @returns {boolean} -- 组建是否需要更新
   */
  BaseComponent.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
    return !(0, _reactImmutableRenderMixin.shallowEqualImmutable)(this.props, nextProps) || !(0, _reactImmutableRenderMixin.shallowEqualImmutable)(this.state, nextState);
  };

  /**
   * 帮助组建中的函数绑定this（组建实例对象）
   * @function bind
   * @memberof BaseComponent
   * @instance
   * @param methods -- 需要绑定this的函数名数组
   * @example bind('func1', 'func2');
   */


  BaseComponent.prototype.bind = function bind() {
    var _this2 = this;

    for (var _len = arguments.length, methods = Array(_len), _key = 0; _key < _len; _key++) {
      methods[_key] = arguments[_key];
    }

    methods.map(function (method) {
      if (_this2[method]) {
        _this2[method] = _this2[method].bind(_this2);
      } else {
        console.warn('\u4E0D\u5B58\u5728\u7684bind:[' + method + ']');
      }
    });
  };

  return BaseComponent;
}(_react.Component);

exports.default = BaseComponent;


BaseComponent.contextTypes = {
  router: _propTypes2.default.object.isRequired
};